/*
 * Copyright (c) 2019-2029, <a href="https://wwww.intellijboy.cn">https://wwww.intellijboy.cn</a> (liuburu@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * <a href="https://www.apache.org/licenses/LICENSE-2.0">https://www.apache.org/licenses/LICENSE-2.0</a>
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package cn.intellijboy.gradle.plugin.task.render;

import cn.intellijboy.gradle.plugin.model.DependencyModel;
import lombok.Getter;
import org.gradle.api.artifacts.ModuleIdentifier;
import org.gradle.api.artifacts.component.ComponentIdentifier;
import org.gradle.api.artifacts.component.LibraryBinaryIdentifier;
import org.gradle.api.artifacts.component.ModuleComponentIdentifier;
import org.gradle.api.artifacts.component.ProjectComponentIdentifier;
import org.gradle.api.tasks.diagnostics.internal.graph.NodeRenderer;
import org.gradle.api.tasks.diagnostics.internal.graph.nodes.RenderableDependency;
import org.gradle.api.tasks.diagnostics.internal.graph.nodes.RenderableDependencyResult;
import org.gradle.internal.logging.text.StyledTextOutput;

import java.util.ArrayList;
import java.util.List;

/**
 * 依赖节点解析渲染器
 *
 * @author intellijboy
 * @since 1.0
 */
public class DependencyNodeRender implements NodeRenderer {

    @Getter
    private final List<DependencyModel> dependencyModels;

    public DependencyNodeRender() {
        this.dependencyModels = new ArrayList<>();
    }

    @Override
    public void renderNode(StyledTextOutput output, RenderableDependency node, boolean alreadyRendered) {
        if (node instanceof RenderableDependencyResult renderableDependencyResult) {
            ComponentIdentifier identifier = renderableDependencyResult.getId();
            if (identifier instanceof ModuleComponentIdentifier moduleComponentIdentifier) {
                ModuleIdentifier moduleIdentifier = moduleComponentIdentifier.getModuleIdentifier();
                DependencyModel dependencyModel = DependencyModel.builder()
                        .id(renderableDependencyResult.getId().toString())
                        .group(moduleIdentifier.getGroup())
                        .name(moduleIdentifier.getName())
                        .version(moduleComponentIdentifier.getVersion())
                        .build();
                dependencyModels.add(dependencyModel);
            }
        } else if (node instanceof ProjectComponentIdentifier projectComponentIdentifier) {
            System.out.println("项目依赖：" + projectComponentIdentifier.getProjectPath());
        } else if (node instanceof LibraryBinaryIdentifier libraryBinaryIdentifier) {
            System.out.println("本地依赖：" + libraryBinaryIdentifier.getProjectPath());
        }

    }
}
